package framework.editor;


import framework.data.Cidade;
import framework.panes.Editor;
import framework.panes.EditorPane;
import java.awt.Frame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FCadCidade.java
 *
 * Created on 03/05/2011, 19:13:02
 */

/**
 *
 * @author usuario
 */
public class CidadeEditor extends EditorPane
{
    private Cidade cidade;
    
    /**
     * 
     * @param Parent
     * @param cidade
     * @return 
     */
    
    public static boolean edit( Frame parent, Cidade cidade )
    {
        CidadeEditor pane = new CidadeEditor();
        
        pane.setCidade( cidade );
        
        return Editor.edit( parent, pane, "Cadastro de Cidade" );
    }

    /** Creates new form FCadCidade */
    public CidadeEditor() 
    {
        initComponents();
    }
    
    /**
     * 
     * @param p 
     */
    private void setCidade( Cidade c )
    {
        this.cidade = c;
        preencherCampos();
    }
    
    /**
     * 
     */
    private void preencherCampos()
    {
        tfCidade.setText( String.valueOf( cidade.getCodigo() ) );
        tfNomeCidade.setText( cidade.getNome() );
        tfEstado.setText( cidade.getEstado() );
        tfPais.setText( cidade.getPais() );
        tfCep.setText( cidade.getCep() );
        tfIbge.setText( String.valueOf( cidade.getIbge() ) );
    }
    
    /**
     * 
     */
    @Override
    public void accept()
    {
        cidade.setNome( tfNomeCidade.getText() );
        cidade.setEstado( tfEstado.getText() );
        cidade.setPais( tfPais.getText() );
        cidade.setCep( tfCep.getText() );
        cidade.setIbge( Integer.parseInt( tfIbge.getText() ) );       
    }
    
    @Override
    public String validateInput()
    {
        String erro = "";
        
        if ( tfNomeCidade.getText().isEmpty())
        {
            erro = " - Cidade inválida.";
        }
        if ( !tfEstado.getText().isEmpty() && tfNomeEstado.getText().isEmpty())
        {
            erro += "\n - Estado inválido.";
        }            
        if ( !tfPais.getText().isEmpty() && tfNomePais.getText().isEmpty())
        {
            erro += "\n - País inválido.";
        }            
        return erro;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        tfCep = new javax.swing.JTextField();
        tfIbge = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        tfEstado = new javax.swing.JTextField();
        tfNomeEstado = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        tfPais = new javax.swing.JTextField();
        tfNomePais = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        tfCidade = new javax.swing.JTextField();
        tfNomeCidade = new javax.swing.JTextField();

        jLabel1.setText("jLabel1");

        setLayout(new java.awt.GridBagLayout());

        jLabel2.setText("Cidade:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(15, 15, 5, 5);
        add(jLabel2, gridBagConstraints);

        jLabel3.setText("Estado:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 5, 5);
        add(jLabel3, gridBagConstraints);

        jLabel4.setText("País:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 5, 5);
        add(jLabel4, gridBagConstraints);

        jLabel5.setText("CEP:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 5, 5);
        add(jLabel5, gridBagConstraints);

        jLabel6.setText("IBGE:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 15, 5);
        add(jLabel6, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        add(tfCep, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 15);
        add(tfIbge, gridBagConstraints);

        jPanel4.setLayout(new java.awt.GridBagLayout());

        tfEstado.setPreferredSize(new java.awt.Dimension(60, 20));
        tfEstado.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfEstadoFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        jPanel4.add(tfEstado, gridBagConstraints);

        tfNomeEstado.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        jPanel4.add(tfNomeEstado, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel4, gridBagConstraints);

        jPanel5.setLayout(new java.awt.GridBagLayout());

        tfPais.setPreferredSize(new java.awt.Dimension(60, 20));
        tfPais.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfPaisFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        jPanel5.add(tfPais, gridBagConstraints);

        tfNomePais.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 15);
        jPanel5.add(tfNomePais, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel5, gridBagConstraints);

        jPanel6.setLayout(new java.awt.GridBagLayout());

        tfCidade.setEnabled(false);
        tfCidade.setPreferredSize(new java.awt.Dimension(60, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 5, 5);
        jPanel6.add(tfCidade, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 5, 15);
        jPanel6.add(tfNomeCidade, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        add(jPanel6, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void tfEstadoFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_tfEstadoFocusLost
    {//GEN-HEADEREND:event_tfEstadoFocusLost
        if ( tfEstado.getText().equalsIgnoreCase( "RO") )
        {
            tfNomeEstado.setText( "Rondônia" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "AC") )
        {
            tfNomeEstado.setText( "Acre" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "AM") )
        {
            tfNomeEstado.setText( "Amazonas" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "RR") )
        {
            tfNomeEstado.setText( "Roraima" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "PA") )
        {
            tfNomeEstado.setText( "Pará" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "AP") )
        {
            tfNomeEstado.setText( "Amapá" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "TO") )
        {
            tfNomeEstado.setText( "Tocantins" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "MA") )
        {
            tfNomeEstado.setText( "Maranhão" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "PI") )
        {
            tfNomeEstado.setText( "Piaui" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "CE") )
        {
            tfNomeEstado.setText( "Ceará" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "RN") )
        {
            tfNomeEstado.setText( "Rio Grande do Norte" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "PB") )
        {
            tfNomeEstado.setText( "Paraíba" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "PE") )
        {
            tfNomeEstado.setText( "Pernambuco" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "AL") )
        {
            tfNomeEstado.setText( "Alagoas" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "SE") )
        {
            tfNomeEstado.setText( "Sergipe" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "BA") )
        {
            tfNomeEstado.setText( "Bahia" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "MG") )
        {
            tfNomeEstado.setText( "Minas Gerais" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "ES") )
        {
            tfNomeEstado.setText( "Espirito Santo" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "RJ") )
        {
            tfNomeEstado.setText( "Rio de Janeiro" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "SP") )
        {
            tfNomeEstado.setText( "São Paulo" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "PR") )
        {
            tfNomeEstado.setText( "Paraná" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "SC") )
        {
            tfNomeEstado.setText( "Santa Catarina" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "RS" ) )
        {
            tfNomeEstado.setText( "Rio Grande do Sul" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "MS") )
        {
            tfNomeEstado.setText( "Mato Grosso do Sul" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "MT") )
        {
            tfNomeEstado.setText( "Mato Grosso" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "GO") )
        {
            tfNomeEstado.setText( "Goias" );
        }
        else if ( tfEstado.getText().equalsIgnoreCase( "DF") )
        {
            tfNomeEstado.setText( "Distrito Federal" );
        }
        else 
        {
            tfNomeEstado.setText( "Desconhecido");
        }
    }//GEN-LAST:event_tfEstadoFocusLost

    private void tfPaisFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_tfPaisFocusLost
    {//GEN-HEADEREND:event_tfPaisFocusLost
       if ( tfPais.getText().equalsIgnoreCase( "BRA") )
       {
           tfNomePais.setText( "Brasil" );
       }
       else 
       {
           tfNomePais.setText( "Desconhecido");
       }
    }//GEN-LAST:event_tfPaisFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JTextField tfCep;
    private javax.swing.JTextField tfCidade;
    private javax.swing.JTextField tfEstado;
    private javax.swing.JTextField tfIbge;
    private javax.swing.JTextField tfNomeCidade;
    private javax.swing.JTextField tfNomeEstado;
    private javax.swing.JTextField tfNomePais;
    private javax.swing.JTextField tfPais;
    // End of variables declaration//GEN-END:variables

}
